#include "kparthread.h"
#include "config.h"

KparThread::KparThread(Kpar* gui)
{
    this->gui = gui;
    engine = 0;
    need_repair = false;
    auto_repair = Config::autoRepair();
    auto_check = Config::autoCheck();
    step = 0;
    connect(this, SIGNAL(finished ()), SLOT(is_finished()));
}
KparThread::~KparThread(){

    delete engine;

}

void KparThread::run()
{
    if (engine == 0)
    {
        engine = new KparEngine(gui);
    }

    if (step == 0)
    {
        engine->loadPAR2Files(file_name);
        step++;
    }
    if (step == 1)
    {
        if (auto_check)
            need_repair = engine->checkParity(file_name);
        step++;
    }
    if (step == 2)
    {
        if (auto_repair && need_repair)
            engine->repairFiles(file_name);
    }
    step = 0;
    auto_repair = Config::autoRepair();
    auto_check = Config::autoCheck();

}

void KparThread::is_finished()
{
    gui->ui.check_Button->setEnabled(true);
    gui->ui.repair_Button->setEnabled(true);
}

void KparThread::start_process(QString file_name)
{
    if( !isRunning() ){
        this->file_name = file_name;
        auto_repair = Config::autoRepair();
        auto_check = Config::autoCheck();
        step = 0;
        start();
    }
}

void KparThread::start_process_check(QString file_name)
{
    if( !isRunning() ){
        this->file_name = file_name;
        auto_repair = Config::autoRepair();
        auto_check = true;
        step = 1;
        start();
    }
}

void KparThread::start_process_repair(QString file_name)
{
    if( !isRunning() ){
        this->file_name = file_name;
        auto_repair = true;
        auto_check = Config::autoCheck();
        step = 2;
        start();
    }
}

